package com.padakeji.android.pworks.module.image.common;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;

import com.padakeji.android.pworks.content.FileUtils;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

/**
 * Created by atanl on 2015/11/20.
 */
public class ImagePickHelper {

    private Context mContext;

    public ImagePickHelper(Context context) {
        mContext = context;
    }

    public Uri convertToFileUri(Uri imageUri) {
        try {
            InputStream is = mContext.getContentResolver().openInputStream(imageUri);
            Bitmap bitmap = BitmapFactory.decodeStream(is);
            is.close();
            File imageFile = FileUtils.saveBitmapToTempFile(bitmap);
            return Uri.fromFile(imageFile);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    public Intent newImagePickIntent() {
        Intent intent = new Intent();
        intent.setType("image/*");
        intent.setAction(Intent.ACTION_GET_CONTENT);
        return intent;
    }

}
